package com.rachel.studyapp.activity;

import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.rachel.studyapp.R;
import com.rachel.studyapp.utils.ToolUtils;

public class WebViewActivity extends BaseActivity {
    private static final String TAG = "zsr";
    private WebView mWebView;
    private ProgressDialog mProgressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_web_view);
        final String url = getIntent().getStringExtra(ToolUtils.HTTPURL);
       // String url = "https://www.baidu.com/";

        Log.d(TAG, "onCreate: "+url);
        mWebView = (WebView) findViewById(R.id.webview);

        //调用本地播放器，但是加载界面不受我们控制
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true); //支持 js
        //设置屏幕自适应
        webSettings.setUseWideViewPort(true); //将图片调整到合适位置
        webSettings.setLoadWithOverviewMode(true); //缩放到屏幕大小
        //缩放操作
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true); //内置缩放软件
        webSettings.setDisplayZoomControls(true);
        //其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式



        if (mWebView != null){
            mWebView.clearCache(true);
            mProgressDialog = ProgressDialog.show(this,null,"页面加载中，请稍后");
            mProgressDialog.setCancelable(true);
            mWebView.loadUrl(url);


            mWebView.setWebViewClient(new WebViewClient(){
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    Log.d(TAG, "onPageFinished: ");
                    mProgressDialog.dismiss();
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
                        Log.d(TAG, "shouldOverrideUrlLoading: "+request.getUrl().toString());
                        view.loadUrl(request.getUrl().toString());
                    }else {
                        Log.d(TAG, "shouldOverrideUrlLoading: "+request.toString());
                        view.loadUrl(request.toString());
                    }
                    return true;
                }
            });
        }


    }

    @Override
    protected void onPause() {
        super.onPause();
        mWebView.onPause();
        mWebView.clearCache(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ViewGroup viewParent = (ViewGroup) mWebView.getParent();
        if (viewParent != null){
            viewParent.removeView(mWebView);
        }
        mWebView.clearCache(true);
        mWebView.destroy();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_web_view;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (mWebView.canGoBack()){
                mWebView.goBack();
                return true; //消费掉back，这样就能按back，网页后退，而不是直接退出
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
